﻿Imports System.Text

Public Class clsPSPLIB
    Private Const TYPE_JOBS = "JOBS"
    Private Const TYPE_RES = "RES"
    Private Const TYPE_PROJ = "PROJ"
    Private Const TYPE_PROJRES = "PROJRES"

    'Todo: Dynamic
    Private RenewableResourceMax As Int32 = 4

    Public Enum eJType
        J30
        J60
        J90
        J120
    End Enum
    Public Function Init() As Boolean
        Try
            Throw New NotImplementedException()
        Catch ex As Exception
            Throw
        End Try
    End Function

    Public Function FileToDataSet(ByVal sFile As String, ByRef projInfo As clsProject, ByVal nType As eJType) As task
        Try
            Dim cds As New clsDataStruc
            Dim i As Int32 = 0
            Dim o As New task
            Dim dt As task.rawDataTable = o.raw
            Dim sType As String = ""
            If IO.File.Exists(sFile) Then
                Dim sr As New IO.StreamReader(sFile)
                Do While Not sr.EndOfStream
                    Dim line As String = sr.ReadLine
                    If line.StartsWith("PRECEDENCE RELATIONS") Then
                        sType = TYPE_JOBS
                    ElseIf line.StartsWith("REQUESTS/DURATIONS") Then
                        sType = TYPE_RES
                    ElseIf line.StartsWith("PROJECT INFORMATION") Then
                        sType = TYPE_PROJ
                    ElseIf line.StartsWith("RESOURCEAVAILABILITIES") Then
                        sType = TYPE_PROJRES
                    ElseIf line.StartsWith("*") Then
                        sType = ""
                    End If
                    Select Case sType
                        Case TYPE_JOBS
                            Dim jobs() As String = System.Text.RegularExpressions.Regex.Split(line, "\s+")
                            If jobs.Length > 1 Then
                                If IsNumeric(jobs(1)) Then
                                    Dim row As task.rawRow = dt.NewrawRow()
                                    Dim j As Int32 = 0
                                    row.key = CLng(jobs(1))
                                    row.task = "Task" & row.key
                                    row.dur = 0
                                    row.pre = ""
                                    Dim post As New StringBuilder()
                                    Dim count As Int32 = CLng(jobs(3))
                                    For j = 4 To count + 3
                                        post.Append(jobs(j))
                                        If j < count + 3 Then post.Append(",")
                                    Next
                                    row.post = post.ToString
                                    dt.AddrawRow(row)
                                End If
                            End If
                        Case TYPE_RES
                            Dim jobs() As String = System.Text.RegularExpressions.Regex.Split(line, "\s+")
                            If jobs.Length > 1 Then
                                Dim x As Int32 = 1
                                If IsNumeric(jobs(x)) Then
                                    If IsNumeric(jobs(0)) Then
                                        'ab 100
                                        x = 0
                                    End If
                                    Dim row As task.rawRow = dt.FindBykey(jobs(x))
                                    row.dur = jobs(x + 2)
                                    Dim renew As New StringBuilder()
                                    For a As Int32 = 1 To RenewableResourceMax
                                        renew.Append("R")
                                        renew.Append(a)
                                        renew.Append("@")
                                        renew.Append(jobs(x + 2 + a))
                                        If a < RenewableResourceMax Then
                                            renew.Append(";")
                                        End If
                                    Next
                                    row.renewableRes = renew.ToString
                                End If
                            End If
                        Case TYPE_PROJ
                            If projInfo Is Nothing Then
                                projInfo = New clsProject
                            End If
                            Dim itms() As String = System.Text.RegularExpressions.Regex.Split(line, "\s+")
                            Dim x As Int32 = 1
                            If IsNumeric(itms(x)) Then
                                If IsNumeric(itms(0)) Then
                                    'ab 100
                                    x = 0
                                End If
                                With projInfo
                                    .Nr = itms(x)
                                    .Jobs = itms(x + 1)
                                    .RelDate = itms(x + 2)
                                    .DueDate = itms(x + 3)
                                    .TardCost = itms(x + 4)
                                    .MPMTime = itms(x + 5)
                                End With
                            End If
                        Case TYPE_PROJRES
                            Dim res() As String = System.Text.RegularExpressions.Regex.Split(line, "\s+")
                            If res.Length > 1 Then
                                If IsNumeric(res(1)) Then
                                    With projInfo
                                        .RenewableResources = New Dictionary(Of String, Int32)
                                        .RenewableResourcesMax = RenewableResourceMax
                                        For a As Int32 = 1 To RenewableResourceMax
                                            .RenewableResources.Add("R" & a, res(a))
                                        Next
                                    End With
                                End If
                            End If
                    End Select
                    i += 1
                Loop
            Else
                Throw New Exception("File " & sFile & " not found!")
            End If
            For Each row As task.rawRow In dt.Rows
                Dim key As Int32 = row.key
                Dim post() As String = row.post.Split(",")
                For Each pos As String In post
                    If pos <> "" Then
                        Dim nextTask As task.rawRow = dt.FindBykey(pos)
                        If Not nextTask Is Nothing Then
                            nextTask.pre &= IIf(nextTask.pre = "", "", ",") & key
                        End If
                    End If
                Next
            Next
            Return o
        Catch ex As Exception
            Throw
        End Try
    End Function

End Class
